package com.estate.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.SessionFactory;

import com.estate.dao.ForumDao;
import com.estate.vo.Vo;
import com.estate.vo.impl.ForumVo;
import com.estate.vo.impl.UserInfoVo;

public class ForumDaoImpl implements ForumDao {
	
	private SessionFactory sessionFactory;
	
	public SessionFactory getSessionFactory(){
		return this.sessionFactory;
	}
	
	public void setSessionFactory(SessionFactory sessionFactory){
		this.sessionFactory=sessionFactory;
	}
	public boolean addVo(Vo vo) {
		this.getSessionFactory().getCurrentSession().save(vo);
		return true;
	}

	public boolean deleteVo(Vo vo) {
		this.getSessionFactory().getCurrentSession().delete(vo);
		return true;
	}

	public List<Vo> findAll() {
		Query query = this.getSessionFactory().getCurrentSession().createQuery("from ForumVo");
		List<Vo> voList = query.list();
		return voList;
	}

	public Vo findByPK(Object pk) {
		Vo vo = (Vo) this.getSessionFactory().getCurrentSession().get(ForumVo.class, (Integer)pk);
		return vo;
	}

	public boolean updateVo(Vo vo) {
		this.getSessionFactory().getCurrentSession().merge(vo);
		return true;
	}


	public List<Vo> findAllMyForumList(UserInfoVo userInfo) {
		Query query = this.getSessionFactory().getCurrentSession().createQuery("from ForumVo where postUser.id=:userId").setInteger("userId", userInfo.getId());
		List<Vo> voList = query.list();
		return voList;
	}

	public List<Vo> findAllPostedForum() {
		Query query = this.getSessionFactory().getCurrentSession().createQuery("from ForumVo where state=:state").setString("state", "已发布");
		List<Vo> voList = query.list();
		return voList;
	}

	public List<ForumVo> getIndexList() {
		Query query = this.getSessionFactory().getCurrentSession().createQuery("from ForumVo where state=:state order by lastCommentDate desc").setString("state", "已发布").setMaxResults(8);
		List<ForumVo> voList = query.list();
		return voList;
	}
}
